package com.springcloud.tools.controller;

import com.alibaba.fastjson.JSON;
import com.springcloud.tools.common.Constance;
import com.springcloud.tools.common.Result;
import com.springcloud.tools.entity.SysRole;
import com.springcloud.tools.service.SysRoleService;
import com.yomahub.aspectlog.annotation.AspectLog;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 角色信息表 前端控制器
 *
 * @author fuc
 * @since 2020-06-10
 */
@RestController
@RequestMapping("/test")
@RefreshScope
@Slf4j
public class SysRoleController {

  @Autowired private SysRoleService sysRoleService;

  @ApiOperation(
      value = "Test测试",
      notes =
          "返回值:</br>"
              + "{</br>"
              + "&emsp;\"roleId\":1，//id</br>"
              + "&emsp;\"roleName\":\"name\",//name</br>"
              + "&emsp;...</br>"
              + "}")
  @ApiImplicitParams({
    @ApiImplicitParam(
        name = "paramMap",
        value = "{" + "\"roleName\":\"name\"，//主键ID}",
        required = true,
        paramType = "body")
  })
  @RequestMapping(value = "/getSysRole", method = RequestMethod.POST)
  @AspectLog({"paramMap.roleName"})
  public Result sysRole(HttpServletRequest request, @RequestBody Map<String, Object> paramMap) {
    Result result = new Result();
    SysRole sysRole = JSON.parseObject(JSON.toJSONString(paramMap), SysRole.class);
    List<SysRole> sysRoleList =
        sysRoleService.lambdaQuery().eq(SysRole::getRoleName, sysRole.getRoleName()).list();
    result.setData(JSON.toJSON(sysRoleList));
    result.setCode(Constance.RESPONSE_SUCCESS);
    result.setMsg("查询信息成功");
    return result;
  }

  @Value("${server.port}")
  private String port;

  @Autowired private Environment environment;

  @RequestMapping(value = "/refreshTest", method = RequestMethod.POST)
  public String refreshTest() {
    return "Hello, welcome to spring cloud 2. environment: "
        + environment.getProperty("server.port")
        + ", value: "
        + port;
  }
}
